package Action;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Aptech.DAL.MgUser;
import Aptech.DALInterface.UserInterface;
import Form.LoginForm;
import HibernateMappingPoJo.Users;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * 
 * @author nkhoan
 */
public class LoginAction extends org.apache.struts.action.Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginForm loginForm = (LoginForm) form;
		String pass = request.getParameter("pass");
		String username = request.getParameter("username");
		String type = request.getParameter("type");

		UserInterface user = new MgUser();

		if (type == null) {
			request.setAttribute("type", "true");
			return mapping.findForward("login");
		} else {
			if (user.getUserByName(username) != null) {
				if (user.getUserByName(username).getPass().toString()
						.equals(pass)) {
					request.getSession().setAttribute("user", username);
					response.sendRedirect("/NTB/user.do?method=getAllUser");
					return mapping.findForward("home");
				}
			} else {
				request.setAttribute("type", "false");
			}
		}
		return mapping.findForward("login");
	}
}
